package tan.dft.quanly;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tan.dft.gps.Danduong;
import tan.dft.gps.R;
import tan.dft.gps.ShowMaps;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class Quanlydanhsachmain extends Activity {
	private ListView listquanly;
	private ArrayList<Quanly> array;
	private QuanlyAdapter adapter;
	private List<Address> addresses;
	private Geocoder geocoder;
	private int idql1, idmayintent;
	private String myjsonstring, name, gender, add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.quanly);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		idmayintent = (Integer) bundle.getInt("idmay");

		listquanly = (ListView) findViewById(R.id.listquanly);
		array = new ArrayList<Quanly>();
		adapter = new QuanlyAdapter(Quanlydanhsachmain.this,
				R.layout.quanlyitem, array);
		// tao mang du lieu
		createArray();

		listquanly.setAdapter(adapter);

		Button btshowall = (Button) findViewById(R.id.btshowall);
		btshowall.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent().setClass(getBaseContext(),
						Danduong.class);
				ArrayList<Quanly> addyExtras = new ArrayList<Quanly>();
				for (int i = 0; i < array.size(); i++) {
					addyExtras.add(array.get(i));
				}
				intent.putParcelableArrayListExtra("mylist", addyExtras);
				startActivity(intent);
			}
		});

		listquanly.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// + dinh nghia 1 intent
				Intent intent = new Intent(Quanlydanhsachmain.this,
						ShowMaps.class);
				// + dinh nghia 1 bunble
				Bundle bundle = new Bundle();
				// + dat cac gia tri can truyen vao bunble
				bundle.putInt("idql1", array.get(arg2).getIdql1());
				bundle.putString("name", array.get(arg2).getName());
				bundle.putDouble("lat", array.get(arg2).getLat());
				bundle.putDouble("lng", array.get(arg2).getLng());
				// + dat bundle vao truong extra cua intent
				intent.putExtras(bundle);
				// + start Activity
				startActivity(intent);
			}

		});
	}

	private void createArray() {

		// Reading text file from assets folder
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"jsondata.txt")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close(); // stop reading
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		myjsonstring = sb.toString();
		try {
			// Creating JSONObject from String
			JSONObject jsonObjMain = new JSONObject(myjsonstring);
			// Creating JSONArray from JSONObject
			JSONArray jsonArray = jsonObjMain.getJSONArray("data");
			// JSONArray has four JSONObject
			for (int i = 0; i < jsonArray.length(); i++) {
				// Creating JSONObject from JSONArray
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				// Getting data from individual JSONObject
				final int idmay = jsonObj.getInt("idmay");

				if (idmayintent == idmay) {

					JSONArray jsonArrayql = jsonObj.getJSONArray("idql");
					for (int j = 0; j < jsonArrayql.length(); j++) {
						JSONObject jsonObjectql = jsonArrayql.getJSONObject(j);
						idql1 = jsonObjectql.getInt("idql1");
						name = jsonObjectql.getString("name");
						gender = jsonObjectql.getString("gender");
						int age = jsonObjectql.getInt("age");
						JSONObject jObj = jsonObjectql.getJSONObject("toado");
						Double lat = jObj.getDouble("lat");
						Double lng = jObj.getDouble("lng");

						geocoder = new Geocoder(this, Locale.getDefault());
						try {
							addresses = geocoder.getFromLocation(lat, lng, 1);
							String address = addresses.get(0).getAddressLine(0);
							String city = addresses.get(0).getAddressLine(1);
							String country = addresses.get(0).getAddressLine(2);
							add = address + " | " + city + " | " + country;

						} catch (IOException e) {
							e.printStackTrace();
						}

						Quanly quanly = new Quanly(idmay, idql1, age, name,
								add, gender, lat, lng);
						array.add(quanly);

					}

				}

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
